package com.zhuoli.service.springboot.test.util.common;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.util.List;
import java.util.Map;

/**
 * @Author: zhuoli
 * @Date: 2018/9/20 12:51
 * @Description:
 */
public class HttpUtils {

    private static final String CHROME_UA = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36";

    public static String getUrl(String urlToRead) throws Exception {
        StringBuilder result = new StringBuilder();
        HttpClient httpclient = HttpClients.createDefault();
        HttpGet g = new HttpGet(urlToRead);
        g.setHeader("User-Agent", CHROME_UA);
        HttpResponse response = httpclient.execute(g);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
        }
        return result.toString();
    }

    public static String postUrl(String urlToRead, List<NameValuePair> params) throws Exception {
        StringBuilder result = new StringBuilder();
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(urlToRead);
        httppost.setHeader("User-Agent", CHROME_UA);
        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
        }
        return result.toString();
    }


    public static String postUrl(String urlToRead, JsonNode jsonNode) throws Exception {
        StringBuilder result = new StringBuilder();
        HttpPost httppost = new HttpPost(urlToRead);

        StringEntity s = new StringEntity(jsonNode.toString(), "application/json", "UTF-8");
        httppost.setEntity(s);
        httppost.setHeader("User-Agent", CHROME_UA);
        HttpClient httpclient = HttpClients.createDefault();
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
        }
        return result.toString();
    }

    public static String postUrl(String urlToRead, Map<String, Object> params, String appName, String appSecret) throws Exception {
        StringBuilder result = new StringBuilder();
        HttpPost httppost = new HttpPost(urlToRead);
        Map<String, String> header = Signer.sign(params, appName, appSecret);
        for (Map.Entry<String, String> e : header.entrySet()) {
            httppost.setHeader(e.getKey(), e.getValue());
        }
        ObjectMapper o = new ObjectMapper();
        ObjectNode node = o.createObjectNode();
        for (Map.Entry<String, Object> e : params.entrySet()) {
            node.put(e.getKey(), e.getValue().toString());
        }
        StringEntity s = new StringEntity(node.toString());
        httppost.setEntity(s);


        HttpClient httpclient = HttpClients.createDefault();
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
        }
        return result.toString();
    }
}
